--- title: RGL + Rmarkdown Tricks author: Susan Vanderplas date: '2019-05-22' categories: - R tags: - note-to-self - useful-code image: caption: '' focal_point: '' --- }}index_files/anchor-sections/anchor-sections.css" rel="stylesheet" /> }}index_files/rglwidgetClass/rgl.css" rel="stylesheet" /> }}index_files/crosstalk/css/crosstalk.css" rel="stylesheet" />
Basic setup - requires x3ptools and rgl packages
library(x3ptools)
library(rgl)
logo <- read_x3p(system.file(package = "x3ptools", "csafe-logo.x3p"))
rgl::setupKnitr()
You can interactively get the viewing angle by using rgl::par3d()$userMatrix
For example, if I use image_x3p(logo) to show the CSAFE logo, and then I rotate the object a bit, I can use
# Not run...
um <- rgl::par3d()$userMatrix
dput(um)
to save the rotation matrix for reproducibility. I can then set it using view3d(userMatrix = um)
Allows you to use RGL and generate a static image automatically. The chunk header looks like this1:
```{r, rgl = TRUE, dev = 'png'}
## Chunk code goes here
```
um <- structure(c(0.990302324295044, -0.108039885759354, -0.0873337239027023,
0, 0.119859784841537, 0.982302069664001, 0.143926605582237, 0,
0.0702383369207382, -0.152998745441437, 0.985726416110992, 0,
0, 0, 0, 1), .Dim = c(4L, 4L)) # from dput()
image_x3p(logo)
rgl::view3d(userMatrix = um)
scene <- rgl::scene3d() # save the scene to an R object
}}index_files/figure-html/unnamed-chunk-2-1.png" width="100%" />
To get RGL embedded in RMarkdown (warning, this will make your HTML files extremely large…), use rgl::rglwidget()
# How to plot:
scene %>%
rglwidget()